package com.stockz.core.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;

public class BarId implements Serializable, Comparable<BarId> {
	private static final long serialVersionUID = -3819901178620766796L;

	private Date date;
	private String code;
	
	public boolean equals(Object o) {
		if(o != null && o instanceof Bar){
			BarId barId = (BarId)o;
			if(code != null && date != null && barId.code != null && barId.date != null){
				return code.trim().equalsIgnoreCase(barId.code.trim()) && date.equals(barId.date);
			}
		}
		return super.equals(o);
	}
	
	@Override
	public int hashCode() {
		if(code != null && date != null){
			return Integer.parseInt(String.valueOf(code.hashCode())+String.valueOf(date.hashCode()));
		}
		return super.hashCode();
	}
	
	public String toString() {
		return date.toString()+code;
	}
	
	public int compareTo(BarId barId) {
		if(code != null && date != null && barId != null && barId.code != null && barId.date != null){
			int codeComparison = code.trim().compareToIgnoreCase(barId.code.trim());
			if(codeComparison != 0){
				return date.compareTo(barId.date);
			}else{
				return codeComparison;
			}
		}
		return 0;
	}
	
	@Column(nullable=false)
	public Date getDate() {
		return date;
	}
	
	@SuppressWarnings("deprecation")
	public void setDate(Date date) {
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		this.date = date;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
}
